package org.rabbit.mq.springbootrabbitconsumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author: zhs
 * @Date: 2021/6/15 14:48
 * @Description:
 */

/**
 * Consumer 限流机制
 * 1.确保 ack 机制为手动确认
 * 2.在listener-container 配置属性
 *  prefetch = 1，表示消费端每次预先从mq拉取一条消息消费，直到手动确认消费完毕后，才会继续去拉取下一条消息
 *
 */
@Component
public class QosListener {

    @RabbitListener( queues = "boot_queue")
    public void listenerQueue(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag,true);
        } catch (IOException e) {
            channel.basicNack(deliveryTag,true,true);
        }
    }

}
